﻿using OpenMms.Domain.Interface;
using OpenMms.Domain.Interface.User;
using OpenMms.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenMms.Domain.BLL.User.Home
{
    /// <summary>
    /// 会员首页服务层
    /// </summary>
    public class HomeService: IHomeService
    {
        #region 从容器获取对象
        /// <summary>
        /// 数据访问
        /// </summary>
        protected IBLLConstruct<UserInforMations> _repository { get; set; }
        /// <summary>
        /// 公告
        /// </summary>
        protected IBLLConstruct<Notice> _notice { get; set; }

        public HomeService(IBLLConstruct<UserInforMations> repository, IBLLConstruct<Notice> notice)
        {
            this._repository = repository;
            this._notice = notice;
        }
        #endregion

        #region 获取用户信息
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public async Task<UserInforMations> GetInfo(string usercode)
        {
           return await _repository.FindAsync(u => u.UserCode == usercode);
        }
        #endregion

        #region 查前五条公告 这个方法不太好,等我有时间优化一下
        /// <summary>
        /// 查询前五条公告
        /// </summary>
        /// <returns></returns>
        public async Task<SortedList<int,Notice>> GetNotices()
        {
            var list = await _notice.QueryAsync(i => i.Id > 0, i => i.Sort, 5);
            var notice = new SortedList<int, Notice>();
            for (int i = 0; i < list.Count; i++)
            {
                notice.Add(i, list[i]);
            }
            return notice;
        }
        #endregion

    }
}
